package co.com.smart.persistence.entity;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import co.com.smart.persistence.entity.dto.SmartPersona;
import co.com.smart.persistence.entity.util.AbstractFacade;

/**
 * Session Bean implementation class PaisFacade
 */
@Stateless(
		name		= "PersonaFacade", 
		mappedName 	= "ejb/PersonaFacade")
public class PersonaFacade  extends AbstractFacade<SmartPersona> implements PersonaFacadeLocal{

	@PersistenceContext(unitName = "SnakeServicesPU")
	private EntityManager em;
	/**
	 * Default constructor. 
	 */
	public PersonaFacade() {
		super(SmartPersona.class);
	}

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}

	@Override
	public SmartPersona consultarPersonaCd(String smDsidentificacion) {
		
		SmartPersona persona = null;
		StringBuffer sql = new StringBuffer("SELECT s FROM SmartPersona s ");
		sql.append("where s.smFhretiro is null and s.smDsidentificacion = :identificacion ");

		Query query = em.createQuery(sql.toString());
		query.setParameter("identificacion", smDsidentificacion);
		try{
			Object object = query.getSingleResult();

			if(object !=  null){
				persona = (SmartPersona) object;
			}
		}catch(NoResultException nr){
			System.out.println("No se encontraron datos");
		}

		return persona;
	}

}
